package Client.ui;


import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import VWorld.Avatar;

import Client.Controler.AppliManager;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JList;

import java.util.HashMap;


public class RoomDetailsWindow {

	private JFrame frame;
	private JTextField tfRoom;
	private AppliManager appliManager;
	private DefaultListModel modelLConnected;
	private HashMap<String, Avatar> nameAvatar;
	private DefaultListModel modelLDisconnected;

	/**
	 * Create the application.
	 */
	public RoomDetailsWindow(AppliManager app) {
		this.appliManager = app;
		
		initialize();
		
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 226, 419);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setTitle("Room details");
		
		JLabel lblSeeRoomN = new JLabel("Room n\u00B0");
		lblSeeRoomN.setBounds(10, 11, 59, 14);
		frame.getContentPane().add(lblSeeRoomN);
		
		JLabel label = new JLabel("Connected:");
		label.setBounds(10, 30, 150, 14);
		frame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("Disconnected:");
		label_1.setBounds(10, 205, 150, 14);
		frame.getContentPane().add(label_1);
		
		JButton btnSeeIt = this.getBSeeIt();
		frame.getContentPane().add(btnSeeIt);
		
		tfRoom = new JTextField();
		tfRoom.setBounds(60, 8, 47, 20);
		frame.getContentPane().add(tfRoom);
		tfRoom.setColumns(10);
		
		
		JList lconnected = this.getLConnected();
		frame.getContentPane().add(lconnected);
		
		
		JList ldisconnected = this.getLDisconnected();
		frame.getContentPane().add(ldisconnected);
	}

	private JList getLDisconnected() {
		this.modelLDisconnected = new DefaultListModel();
		final JList ldisconnected = new JList(this.modelLDisconnected);
		ldisconnected.setBounds(10, 219, 190, 150);
		
		ldisconnected.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				Object ob[] = ldisconnected.getSelectedValues();
				displayInfo(nameAvatar.get(ob[0]));
			}
		});
		
		return ldisconnected;
	}

	private JList getLConnected() {
		this.modelLConnected = new DefaultListModel();
		final JList lconnected = new JList(this.modelLConnected);
		lconnected.setBounds(10, 44, 190, 150);
		
		lconnected.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				Object ob[] = lconnected.getSelectedValues();
				displayInfo(nameAvatar.get(ob[0]));
			}
		});
		
		return lconnected;
	}

	private void displayInfo(Avatar avatar) {
		new CreateAvatarWindow(appliManager, avatar, false, true);
	}
	
	private JButton getBSeeIt() {
		JButton btnSeeIt = new JButton("See it");
		btnSeeIt.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				try{
					modelLConnected.removeAllElements();
					modelLDisconnected.removeAllElements();
					nameAvatar = new HashMap<String, Avatar>();
					Avatar [] avatars = appliManager.seeAvatars(new Integer(tfRoom.getText()).intValue());
					for(Avatar av : avatars){
						nameAvatar.put(av.name, av);
						if(av.estConnecte){
							modelLConnected.addElement(av.name);
						}else{
							modelLDisconnected.addElement(av.name);
						}
					}
				}catch(NumberFormatException nfe){
					
				}
			}
		});
		btnSeeIt.setBounds(113, 7, 89, 23);
		return btnSeeIt;
	}
}
